<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

/**
 * BatchItem
 *
 * Base class for the contents of a batch.  Subclasses created based on value
 * of "bitem_type".
 *
 * @property integer $bitem_id
 * @property integer $bitem_batch_id
 * @property string $bitem_type
 * @property integer $bitem_xid
 * @property string $bitem_status
 * @property integer $bitem_cre_user
 * @property integer $bitem_upd_user
 * @property timestamp $bitem_cre_dtim
 * @property timestamp $bitem_upd_dtim
 * @property Batch $Batch
 * @property Doctrine_Collection $BatchRelated
 * @author rcavis
 * @package default
 */
class BatchItem extends AIR2_Record {
    /* code_master values */
    public static $STATUS_ACTIVE = 'A';
    public static $STATUS_INACTIVE = 'F';

    /**
     * Set the table columns
     */
    public function setTableDefinition() {
        $this->setTableName('batch_item');
        $this->hasColumn('bitem_id', 'integer', 4, array(
                'primary' => true,
                'autoincrement' => true,
            ));
        $this->hasColumn('bitem_batch_id', 'integer', 4, array(
                'notnull' => true,
            ));
        $this->hasColumn('bitem_type', 'string', 1, array(
                'fixed' => true,
                'notnull' => true,
            ));
        $this->hasColumn('bitem_xid', 'integer', 4, array(
                'notnull' => true,
            ));
        $this->hasColumn('bitem_status', 'string', 1, array(
                'fixed' => true,
                'notnull' => true,
                'default' => self::$STATUS_ACTIVE,
            ));
        $this->hasColumn('bitem_cre_user', 'integer', 4, array(
                'notnull' => true,
            ));
        $this->hasColumn('bitem_upd_user', 'integer', 4, array(

            ));
        $this->hasColumn('bitem_cre_dtim', 'timestamp', null, array(
                'notnull' => true,
            ));
        $this->hasColumn('bitem_upd_dtim', 'timestamp', null, array(

            ));

        parent::setTableDefinition();

        // unique index to prevent duplicate items in a batch
        $this->index('bitem_uniqueidx_1', array(
                'fields' => array('bitem_batch_id', 'bitem_type', 'bitem_xid'),
                'type' => 'unique'
            ));

        // DON'T prevent exporting constraints here ... do it in the SUBCLASSES
        //$this->setAttribute(Doctrine_Core::ATTR_EXPORT, Doctrine_Core::EXPORT_TABLES);

        // column aggregate inheritance
        $this->setSubClasses(array(
                'BatchSource' => array('bitem_type' => BATCH::$TYPE_SOURCE),
                'BatchUser' => array('bitem_type' => BATCH::$TYPE_USER),
            ));
    }


    /**
     * Set table relations
     */
    public function setUp() {
        parent::setUp();
        $this->hasOne('Batch', array(
                'local' => 'bitem_batch_id',
                'foreign' => 'batch_id',
                'onDelete' => 'CASCADE'
            ));
        $this->hasMany('BatchRelated', array(
                'local' => 'bitem_id',
                'foreign' => 'brel_bitem_id'
            ));
    }


    /**
     * Add custom search query (from the get param 'q')
     *
     * @param AIR2_Query $q
     * @param string  $alias
     * @param string  $search
     * @param boolean $useOr
     */
    public static function add_search_str(&$q, $alias, $search, $useOr=null) {
        $d = $q->getDql();
        if (preg_match('/BatchSource/', $d)) {
            BatchSource::add_search_str ($q, null, $search, $useOr);
        }
        if (preg_match('/BatchUser/', $d)) {
            BatchUser::add_search_str ($q, null, $search, $useOr);
        }
    }


}
